package com.efounder.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.efounder.adapter.AddressAdapter;
import com.efounder.bean.Address;
import com.efounder.mobilemall.R;

import java.io.Serializable;
import java.util.List;

/**
 * @deprecated 新的MallAddressListActivity
 * Created by will on 18-2-8.
 * 展示收货地址列表，上部透明
 */

public class MallShowAddressActivity extends Activity implements View.OnClickListener{

    private List<Address> addresses;
    private Address targetAddress;
    public static final int ADD_ADDRESS_REQUEST_CODE = 8803;
    public static final int EDIT_ADDRESS_REQUEST_CODE = 8804;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_address_list);
        Bundle bundle = getIntent().getExtras();
        addresses = (List<Address>)bundle.get("address");
        targetAddress = (Address) bundle.get("targetAddress");
        initView();
    }

    public void initView() {
        View transparentView = (View) findViewById(R.id.transparent_view);
        transparentView.setOnClickListener(this);
        ImageView ivBack = (ImageView) findViewById(R.id.iv_back);
        ivBack.setOnClickListener(this);
        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        final AddressAdapter addressAdapter = new AddressAdapter(addresses, this);
        recyclerView.setAdapter(addressAdapter);
        addressAdapter.setOnShowAddAddressPopupListener(new AddressAdapter.OnShowAddAddressPopupListener() {
            @Override
            public void onShowAddAddressPop() {
                Intent intent = new Intent(MallShowAddressActivity.this, MallAddAddressActivity.class);
                startActivityForResult(intent, ADD_ADDRESS_REQUEST_CODE);
            }
        });
        addressAdapter.setOnAddressSelectedListener(new AddressAdapter.OnAddressSelectedListener() {
            @Override
            public void onAddressSelected(int position) {
                for (int i = 0; i < addresses.size(); i++) {
                    if (i == position){
                        addresses.get(i).setCurrentSelected(true);
                    }else {
                        addresses.get(i).setCurrentSelected(false);
                    }
                }
                addressAdapter.notifyDataSetChanged();
            }
        });
        addressAdapter.setOnShowSubmitPopupListener(new AddressAdapter.OnShowSubmitPopupListener() {
            @Override
            public void onShowSubmitPop() {

            }
        });
        addressAdapter.setOnShowEditPopupListener(new AddressAdapter.OnShowEditAddressPopupListener() {
            @Override
            public void onShowEditPop(int position) {
                Intent intent = new Intent(MallShowAddressActivity.this, MallEditAddressActivity.class);
                intent.putExtra("address", (Serializable) addresses);
                intent.putExtra("position", position);
                startActivityForResult(intent, EDIT_ADDRESS_REQUEST_CODE);
            }
        });
        TextView tvSave = (TextView) findViewById(R.id.tv_save_address);
        tvSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.putExtra("addresses", (Serializable)addresses);
                setResult(RESULT_OK, intent);
                finish();
            }
        });
    }

    @Override
    public void finish() {
        super.finish();
        this.overridePendingTransition(R.anim.bottom_dialog_exit,0);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case ADD_ADDRESS_REQUEST_CODE:
                if (resultCode == RESULT_OK) {
                    String addressId = (String) data.getExtras().getString("addressId");
                    Intent intent = new Intent();
                    intent.putExtra("addressId", addressId);
                    setResult(RESULT_OK, intent);
                    finish();
                }
                break;
            case EDIT_ADDRESS_REQUEST_CODE:
                if (resultCode == RESULT_OK) {
                    String addressId = (String) data.getExtras().getString("addressId");
                    Intent intent = new Intent();
                    intent.putExtra("addressId", addressId);
                    setResult(RESULT_OK, intent);
                    finish();
                }
                break;
        }
    }

    @Override
    public void onClick(View view) {

    }
}
